//@charset "utf-8";
(function($) {
	var defaults = {
		contentWidth: 620,
		positions: 'left'
	};

	var nextScrollTo = function(self) {
		if (!self.ulDom.is(':animated')) {
			var ulCurLeft = Math.abs(parseInt(self.ulDom.css(self.positions)));
			var liSum = 0;
			var scrollNum = 0;

			$.each(self.liList, function(k, v) {
				liSum += $(this).outerWidth(true);
				if (liSum > self.contentWidth + ulCurLeft) {

					if (self.ulWidth - ulCurLeft <= self.contentWidth) {
						self.nextBtn.off('click');
						self.prevBtn.off('click');
						self.prevBtn.on('click', function() {
							prevScrollTo(self);
						});
						return;
					} else {
						self.nextBtn.off('click');
						self.nextBtn.on('click', function() {
							//console.log(123333);
							nextScrollTo(self);
						});
					}

					scrollNum = liSum - self.contentWidth;
					self.ulDom.animate({
						left: '-' + scrollNum + 'px'
					});
					return false;
				}
			});

		}
	};
	var prevScrollTo = function(self) {
		if (!self.ulDom.is(':animated')) {
			var ulCurLeft = Math.abs(parseInt(self.ulDom.css(self.positions)));
			var liSum = 0;
			var scrollNum = 0;
			if (ulCurLeft === 0) {
				//不能点击
				self.prevBtn.off('click');
				return;
			}
			if (self.positions == "left") {
				$.each(self.liList, function(index) {
					liSum += $(this).outerWidth(true);

					if (liSum >= ulCurLeft) {

						if (self.ulWidth - ulCurLeft >= self.contentWidth) {
							self.nextBtn.off();
							self.nextBtn.on('click', function() {
								nextScrollTo(self);
							});
						} else {
							self.prevBtn.off();
							self.prevBtn.on('click', function() {
								prevScrollTo(self);
							})
						}

						scrollNum = liSum - $(this).outerWidth(true);
						self.ulDom.animate({
							"left": '-' + scrollNum + 'px'
						});
						return false;
					}
				});
			}


		}
	};
	$.fn.labelScroll = function(options) {
		var settings = $.extend(defaults, options || {});
		return this.each(function() {
			var $this = $(this);
			var ulWidth = 0;
			$this.find('ul>li').each(function() {
				ulWidth += $(this).outerWidth(true);
			});

			$this.ulWidth = parseInt(ulWidth);
			$this.prevBtn = $this.find('.label-prev-btn');
			$this.nextBtn = $this.find('.label-next-btn');
			$this.ulDom = $this.find('ul');

			$this.contentWidth = $this.find('.labels-box').width(); //settings.contentWidth;
			$this.liList = $this.ulDom.find('li');
			$this.listLength = $this.liList.length;
			$this.positions = settings.positions;
			$this.ulLeft = Math.abs(parseInt($this.ulDom.css($this.positions)));

			if (ulWidth - $this.ulLeft >= parseInt($this.contentWidth)) {
				//$this.find('.label-next-btn, .label-prev-btn').show();
				$this.on({
					mouseover: function(e) {
						$this.prevBtn.removeClass('hide');
						$this.nextBtn.removeClass('hide');
					},
					mouseout: function(e) {
						$this.prevBtn.addClass('hide');
						$this.nextBtn.addClass('hide');
					}
				});

			} else {
				$this.off();
			}

			$this.prevBtn.off();
			$this.nextBtn.off();
			$this.prevBtn.on('click', function() {
				prevScrollTo($this);
			});
			$this.nextBtn.on('click', function() {
				nextScrollTo($this);
			});
			$this.ulDom.css({
				'left': 0
			});



		});

	}
})(jQuery);